﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;


namespace Dragon.Prototype1.Infrastructure.Controls
{
    public enum PopupBoxMode
    { 
        multiselect,
        singleselect
    }

    public class PopupBox:jQueryControl
    {
        public PopupBox(string id)
        {
            this.ID = id;
            this.Name = id;
            this.TextBoxID = id + "_text";
            this.HiddenBoxID = id+"_hidden";
            PopupCallbackFunctionName = "set_return";
            FieldToControlNameMapping = new Dictionary<string, string>();
            this.ActionName = "Popup";
            this.Mode = PopupBoxMode.singleselect;
            this.ClosePopupAfterSelect = true;
        }

        protected override System.Web.UI.HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Unknown;
            }
        }
        public override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            this.OnPreRender(writer);
            this.RenderContents(writer);
        }
        public string TextBoxID
        {
            get;
            set;
        }
        public string HiddenBoxID
        {
            get;
            set;
        }
        public string Text
        {
            get;
            set;
        }

        public PopupBoxMode Mode
        {
            get;
            set;
        }

        public string HiddenValue
        {
            get;
            set;
        }
        public int PopupWidth
        {
            get;
            set;
        }
        public int PopupHeight
        {
            get;
            set;
        }
        public bool ClosePopupAfterSelect
        {
            get;
            set;
        }
        public string FormName
        {
            get;
            set;
        }
        public string PopupCallbackFunctionName
        {
            get;
            set;
        }
        public string ModuleName
        {
            get;
            set;
        }
        public string ActionName
        {
            get;
            set;
        }
        public Dictionary<string, string> FieldToControlNameMapping
        {
            get;
            set;
        }

        public override void RenderContents(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            //render textbox
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.TextBoxID);
            writer.AddAttribute(HtmlTextWriterAttribute.Name,this.TextBoxID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
            writer.AddStyleAttribute("float", "left");
            if (!string.IsNullOrEmpty(this.Text))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Value,Microsoft.Security.Application.AntiXss.HtmlEncode(this.Text));
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            //render hidden input
            if(!string.IsNullOrEmpty(this.HiddenBoxID))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.HiddenBoxID);
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.HiddenBoxID);
            }
            if (!string.IsNullOrEmpty(this.HiddenValue))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Value, this.HiddenValue);
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Type,"hidden");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();

            //render the search icon
            RenderIcon(writer, "ui-icon-search", this.ID + "_searchbutton");
            RenderIcon(writer, "ui-icon-trash", this.ID + "_clearbutton");
            
            //add a clear:Both div
            writer.AddStyleAttribute("clear", "both");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
            writer.RenderEndTag();//div end
        }
        private void RenderIcon(HtmlTextWriter writer,string iconStyle,string iconId)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ui-state-default ui-corner-all");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "18px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "18px");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, iconId);
            writer.AddStyleAttribute("float", "left");
            writer.AddStyleAttribute("margin-left", "2px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ui-icon " + iconStyle);
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        public override void RenderJavaScriptContents(HtmlTextWriter writer)
        {
            base.RenderJavaScriptContents(writer);
            writer.WriteLine(string.Format("jQuery(\"#{0}\").click(function(){{",this.ID+"_searchbutton"));
            StringBuilder requestdata = new StringBuilder();
            requestdata.AppendFormat("{{\"call_back_function\":\"{0}\",",this.PopupCallbackFunctionName);
            if (!string.IsNullOrEmpty(this.FormName))
            {
                requestdata.AppendFormat("\"form_name\":\"{0}\",", this.FormName);
            }
            if (this.FieldToControlNameMapping.Count > 0)
            {
                requestdata.Append("\"field_to_name_array\":{");
                int i = 1;
                foreach (KeyValuePair<string, string> pair in this.FieldToControlNameMapping)
                {
                    if(i==this.FieldToControlNameMapping.Count)
                        requestdata.AppendFormat("\"{0}\":\"{1}\"", pair.Key, pair.Value);
                    else
                        requestdata.AppendFormat("\"{0}\":\"{1}\",",pair.Key,pair.Value);
                    i++;
                }
                requestdata.Append("}");
            }
            requestdata.Append("}");
            writer.WriteLine(string.Format("open_popup(\"/{0}/{1}\",{2},{3},{4},{5},{6},'{7}');",
                this.ModuleName,this.ActionName,
                this.PopupWidth,
                this.PopupHeight,
                this.GetJavaScriptValueString(this.ClosePopupAfterSelect),
                this.GetJavaScriptValueString(false),
                requestdata.ToString(),
                this.Mode.ToString()));
            writer.WriteLine("});");

            writer.WriteLine(string.Format("jQuery(\"#{0}\").click(function(){{", this.ID + "_clearbutton"));
            foreach (string fieldname in this.FieldToControlNameMapping.Values)
            {
                writer.WriteLine(string.Format("jQuery(\"#{0}\").val(\"\");",fieldname));
            }
            writer.WriteLine("});");
        }
    }
}
